﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using V5_DAL;
using System.Data;
using V5_Model;
using V5_Common;

namespace V5_BLL
{
    public class BLLArticleClass
    {
        private DALArticleClass dal = new DALArticleClass();
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ClassID)
        {
            return dal.Exists(ClassID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelArticleClass model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ModelArticleClass model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ClassID)
        {
            ModelArticleClass model = this.GetModel(ClassID);
            if (model.ParentID > 0)
            {
                ModelArticleClass model1 = this.GetModel(model.ParentID);
                model1.Child = model1.Child - 1;
                this.Update(model1);
            }
            //
            dal.Delete(ClassID);
            //文章删除 可以添加 回收站  分类 为0 名称为"未归档"
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelArticleClass GetModel(int ClassID)
        {

            return dal.GetModel(ClassID);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public ModelArticleClass GetModelByCache(int ClassID)
        {

            string CacheKey = "S_ClassModel-" + ClassID;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ClassID);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (ModelArticleClass)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(string strTop, string strWhere, string filedOrder)
        {
            return dal.GetList(strTop, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelArticleClass> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ModelArticleClass> DataTableToList(DataTable dt)
        {
            List<ModelArticleClass> modelList = new List<ModelArticleClass>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                ModelArticleClass model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new ModelArticleClass();
                    if (dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["ChannelID"].ToString() != "")
                    {
                        model.ChannelID = int.Parse(dt.Rows[n]["ChannelID"].ToString());
                    }
                    model.ClassName = dt.Rows[n]["ClassName"].ToString();
                    if (dt.Rows[n]["ParentID"].ToString() != "")
                    {
                        model.ParentID = int.Parse(dt.Rows[n]["ParentID"].ToString());
                    }
                    model.ParentPath = dt.Rows[n]["ParentPath"].ToString();
                    if (dt.Rows[n]["Depth"].ToString() != "")
                    {
                        model.Depth = int.Parse(dt.Rows[n]["Depth"].ToString());
                    }
                    if (dt.Rows[n]["RootID"].ToString() != "")
                    {
                        model.RootID = int.Parse(dt.Rows[n]["RootID"].ToString());
                    }
                    if (dt.Rows[n]["Child"].ToString() != "")
                    {
                        model.Child = int.Parse(dt.Rows[n]["Child"].ToString());
                    }
                    if (dt.Rows[n]["OrderID"].ToString() != "")
                    {
                        model.OrderID = int.Parse(dt.Rows[n]["OrderID"].ToString());
                    }
                    //model.ClassKeyWords = dt.Rows[n]["ClassKeyWords"].ToString();
                    //model.ClassDesscription = dt.Rows[n]["ClassDesscription"].ToString();
                    model.ReadMe = dt.Rows[n]["ReadMe"].ToString();
                    if (dt.Rows[n]["AddDateTime"].ToString() != "")
                    {
                        model.AddDateTime = DateTime.Parse(dt.Rows[n]["AddDateTime"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        #endregion  成员方法

        public DataTable GetClassList(int ChannelID)
        {
            DataTable table = new DataTable();
            DataRow row = null;
            string strWhere = "";
            table.Columns.Add("ClassName");
            table.Columns.Add("ClassID");
            if (ChannelID != 0)
            {
                strWhere = " ChannelID=" + ChannelID;
            }
            DataView dv = this.GetList("", strWhere, "RootID,OrderID").Tables[0].DefaultView;
            if (dv != null && dv.Count > 0)
            {
                for (int i = 0; i < dv.Count; i++)
                {
                    string strTemp = "";
                    int iDepth = 0, iChild = 0;
                    iDepth = Input.SetInt(dv[i]["Depth"].ToString());
                    if (iDepth > 0)
                    {
                        for (int j = 0; j < iDepth; j++)
                        {
                            strTemp += "　";
                        }
                    }
                    iChild = Input.SetInt(dv[i]["Child"].ToString());
                    if (iChild > 0)
                    {
                        strTemp += "+";
                    }
                    else
                    {
                        strTemp += "-";
                    }
                    row = table.NewRow();
                    row["ClassID"] = dv[i]["ClassID"].ToString();
                    row["ClassName"] = strTemp + dv[i]["ClassName"].ToString();
                    table.Rows.Add(row);
                }
            }
            return table;
        }

        public DataTable GetDropDownList(string Msg, int ChannelID)
        {
            DataTable table = new DataTable();
            DataRow row = null;
            string strWhere = "";
            table.Columns.Add("ClassName");
            table.Columns.Add("ClassID");
            if (Msg.Trim() != "")
            {
                row = table.NewRow();
                row["ClassName"] = Msg;
                row["ClassID"] = "0";
                table.Rows.Add(row);
            }
            if (ChannelID != 0)
            {
                strWhere = " ChannelID=" + ChannelID;
            }
            DataView dv = this.GetList("", strWhere, " RootID,OrderID ").Tables[0].DefaultView;
            if (dv != null && dv.Count > 0)
            {
                for (int i = 0; i < dv.Count; i++)
                {
                    int tmpDepth = 0;
                    string strTemp = "";
                    tmpDepth = Input.SetInt(dv[i]["Depth"]);
                    if (tmpDepth > 0)
                    {
                        for (int j = 0; j <= tmpDepth; j++)
                        {
                            if (j == tmpDepth)
                            {
                                strTemp += "├";
                            }
                            else
                            {
                                strTemp += "　";
                            }
                        }
                    }
                    row = table.NewRow();
                    row["ClassID"] = dv[i]["ClassID"].ToString();
                    row["ClassName"] = strTemp + dv[i]["ClassName"].ToString();
                    table.Rows.Add(row);
                }
            }
            return table;
        }

        public int GetMaxId(string fileName, string tableName)
        {
            return dal.GetMaxId(fileName, tableName);
        }

        public int GetMaxId(string fileName, string tableName, string whereStr)
        {
            return dal.GetMaxId(fileName, tableName, whereStr);
        }

        public int ExecuteSql(string sql)
        {
            return dal.ExecuteSql(sql);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelArticleClass model, bool fliter)
        {
            int PrevOrderID = 0, ParentDepth = 0, Child = 0, Depth = 0;
            string ParentPath = "";
            int RootID = this.GetMaxId("RootID", "K_ArticleClass");
            if (model.ParentID > 0)
            {
                ModelArticleClass smodel = this.GetModel(model.ParentID);
                if (smodel != null)
                {
                    RootID = smodel.RootID;
                    ParentDepth = smodel.Depth;
                    ParentPath = smodel.ParentPath;
                    Child = smodel.Child;
                    ParentPath = ParentPath + "," + model.ParentID;
                    PrevOrderID = smodel.OrderID;//排序ID
                    if (Child > 0)
                    {
                        PrevOrderID = this.GetMaxId("OrderID", "K_ArticleClass", "ParentID=" + model.ParentID);
                    }
                }
                Depth = ParentDepth + 1;//深度ID
            }
            else
            {
                PrevOrderID = 0;
                ParentPath = "0";
                Depth = 0;
            }
            model.RootID = RootID;
            model.ParentPath = ParentPath;
            model.Depth = Depth;
            model.OrderID = PrevOrderID;
            model.ClassID = this.GetMaxId();
            dal.Add(model);
            if (model.ParentID > 0)
            {
                this.ExecuteSql("update K_ArticleClass set child=child+1 where  ClassID=" + model.ParentID);

                this.ExecuteSql("update K_ArticleClass set OrderID=OrderID+1 where  rootid=" + RootID + " and OrderID>" + PrevOrderID);

                this.ExecuteSql("update K_ArticleClass set OrderID=" + PrevOrderID + "+1 where ClassID=" + model.ClassID);

            }
        }
    }
}
